package com.china.osourcechina;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

/**
 * Created by 孙肖雪 on 2016/5/12.
 */
public class CharacterActivity extends Activity implements View.OnClickListener{
    private ImageView character_image1,character_image2,character_image3;
    private int RESULT_LOAD_IMAGE = 200;
    private int CAMERA_RESULT = 200;
    private File mPhotoFile;
    private Bitmap photo;
    private String saveDir = Environment.getExternalStorageDirectory()
            .getPath() + "/temp_image";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_character);
        //调用找空件的方法
        initview();
        Intent it=getIntent();
        int i=it.getIntExtra("arg2", 0);
        if (i==0){
           // introduction_web.loadUrl(Urls.detailintroductionurl);
        }else if (i==1){
            String state = Environment.getExternalStorageState();
            if (state.equals(Environment.MEDIA_MOUNTED)) {
                mPhotoFile = new File(saveDir, "temp.jpg");
                mPhotoFile.delete();
                if (!mPhotoFile.exists()) {
                    try {
                        mPhotoFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplication(), "照片创建失败!",
                                Toast.LENGTH_LONG).show();
                        return;
                    }
                }
                Intent intent = new Intent(
                        "android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(mPhotoFile));
                startActivityForResult(intent, CAMERA_RESULT);
            } else {
                Toast.makeText(getApplication(), "sdcard无效或没有插入!",
                        Toast.LENGTH_SHORT).show();
            }
        }else if (i==2){
            Intent intent2 = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent2, RESULT_LOAD_IMAGE);
            File savePath = new File(saveDir);
            if (!savePath.exists()) {
                savePath.mkdirs();
            }
        }

    }

    //找空件
    private void initview() {
        character_image1=(ImageView) findViewById(R.id.character_image1);
        character_image2=(ImageView) findViewById(R.id.character_image2);
        character_image3=(ImageView) findViewById(R.id.character_image3);
        character_image1.setOnClickListener(this);
        character_image2.setOnClickListener(this);
        character_image3.setOnClickListener(this);
    }

    //加监听
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.character_image1:
                finish();
                break;
            case R.id.character_image2:

                break;

            case R.id.character_image3:

                break;
            default:
                break;
        }
    }

    //相机拍照接口回调显示图片
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_RESULT && resultCode == RESULT_OK) {
            if (mPhotoFile != null && mPhotoFile.exists()) {
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmapOptions.inSampleSize = 2;
                int degree = readPictureDegree(mPhotoFile.getAbsolutePath());
                Bitmap bitmap = BitmapFactory.decodeFile(mPhotoFile.getPath(),
                        bitmapOptions);
                bitmap = rotaingImageView(degree, bitmap);
                character_image3.setImageBitmap(bitmap);
            }
        }

        //从相册中选择图片展示
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            character_image3.setImageBitmap(BitmapFactory
                    .decodeFile(picturePath));
        }
    }

    private static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    private static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        // 旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

}
